package com.lei.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lei.eduservice.entity.EduVideo;
import com.lei.eduservice.feign.VodClient;
import com.lei.eduservice.mapper.EduVideoMapper;
import com.lei.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author 熊磊
 * @since 2020-06-04
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    private VodClient vodClient;
    @Override
    public void removeByCourseId(String courseId) {
        //删小节的同时删除阿里云视频
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.select("video_source_id");
        System.out.println(courseId);
        List<EduVideo> eduVideos = baseMapper.selectList(wrapper);
        ArrayList<String> videoIdList = new ArrayList<>();



        //封装VideoId过程
        if (eduVideos.size()>0){
            for (EduVideo eduVideo : eduVideos) {
                if (!StringUtils.isEmpty(eduVideo.getVideoSourceId())){
                    videoIdList.add(eduVideo.getVideoSourceId());
                }
            }
        }

        //删除视频
        vodClient.deleteVideoBatchIds(videoIdList);
        //删除小节
        baseMapper.delete(wrapper);



    }
}
